package client.comm.commlib.utils

import android.net.Uri
import androidx.annotation.IntRange
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import client.comm.commlib.comm_ui.PhotoFragment

class PhotoUtils {

    companion object {

        /**
         * 调用裁剪功能
         */
        @JvmStatic
        fun crop(manager: FragmentManager): Crop = Crop(manager)

        /**
         * 调用裁剪功能
         */
        @JvmStatic
        fun crop(activity: FragmentActivity): Crop = Crop(activity.supportFragmentManager)

        /**
         * 调用裁剪功能
         */
        @JvmStatic
        fun crop(fragment: Fragment): Crop = Crop(fragment.childFragmentManager)


        class Crop(private val manager: FragmentManager) {

            private var aspectX = 0
            private var aspectY = 0
            private var outputX = 0
            private var outputY = 0

            /**
             * 设置比例
             */
            fun setAspect(
                @IntRange(from = 0) aspectX: Int,
                @IntRange(from = 0) aspectY: Int
            ): Crop {
                this.aspectX = aspectX
                this.aspectY = aspectY
                return this
            }

            /**
             * 设置输出图片的宽高
             */
            fun setOutput(@IntRange(from = 0) width: Int, @IntRange(from = 0) height: Int): Crop {
                this.outputX = width
                this.outputY = height
                return this
            }

            fun build(
                uri: Uri,
                cropCallBack: (cropUri: Uri?, success: Boolean, msg: String) -> Unit
            ) {
                crop(manager, uri, aspectX, aspectY, outputX, outputY, cropCallBack)
            }
        }

        /**
         * 打开相册
         */
        @JvmStatic
        fun select(
            manager: FragmentManager,
            photoCallBack: (uri: Uri?, success: Boolean, msg: String) -> Unit
        ) = getPhotoFragment(manager).select(photoCallBack)

        /**
         * 打开相册
         */
        @JvmStatic
        fun select(
            activity: FragmentActivity,
            photoCallBack: (uri: Uri?, success: Boolean, msg: String) -> Unit
        ) = getPhotoFragment(activity.supportFragmentManager).select(photoCallBack)

        /**
         * 打开相册
         */
        @JvmStatic
        fun select(
            fragment: Fragment,
            photoCallBack: (uri: Uri?, success: Boolean, msg: String) -> Unit
        ) = getPhotoFragment(fragment.childFragmentManager).select(photoCallBack)

        /**
         * 调用相机拍照
         */
        @JvmStatic
        fun camera(
            manager: FragmentManager,
            photoCallBack: (uri: Uri?, success: Boolean, msg: String) -> Unit
        ) = getPhotoFragment(manager).camera(photoCallBack)

        /**
         * 调用相机拍照
         */
        @JvmStatic
        fun camera(
            activity: FragmentActivity,
            photoCallBack: (uri: Uri?, success: Boolean, msg: String) -> Unit
        ) = getPhotoFragment(activity.supportFragmentManager).camera(photoCallBack)

        /**
         * 调用相机拍照
         */
        @JvmStatic
        fun camera(
            fragment: Fragment,
            photoCallBack: (uri: Uri?, success: Boolean, msg: String) -> Unit
        ) = getPhotoFragment(fragment.childFragmentManager).camera(photoCallBack)

        private fun crop(
            manager: FragmentManager,
            uri: Uri,
            aspectX: Int,
            aspectY: Int,
            outputX: Int,
            outputY: Int,
            cropCallBack: (Uri?, Boolean, String) -> Unit
        ) = getPhotoFragment(manager).crop(uri, aspectX, aspectY, outputX, outputY, cropCallBack)

        private fun getPhotoFragment(manager: FragmentManager) =
            manager.findFragmentByTag("photoFragmen") as? PhotoFragment ?: PhotoFragment().apply {
                manager.beginTransaction()
                    .add(this, "photoFragmen")
                    .commitAllowingStateLoss()
                manager.executePendingTransactions()
            }
    }


}